# -*- coding: utf-8 -*-
import jwt
import time
from django.conf import settings
from rest_framework.authentication import BaseAuthentication, get_authorization_header
from rest_framework import exceptions
from .models import OAUser


def generate_jwt(user):
    timestamp = int(time.time()) + 100000000000000000
    token = jwt.encode({"userid": user.uid, "exp": timestamp}, key=settings.SECRET_KEY, algorithm="HS256")
    return token


class UserTokenAuthentication(BaseAuthentication):
    def authenticate(self, request):
        return request._request.user, request._request.auth


class JWTAuthentication(BaseAuthentication):
    keyword = 'JWT'
    model = None

    def authenticate(self, request):
        auth = get_authorization_header(request).split()

        if not auth or auth[0].lower() != self.keyword.lower().encode():
            return None

        if len(auth) == 1:
            msg = '没有提供JWToken！'
            raise exceptions.AuthenticationFailed(msg)
        elif len(auth) > 2:
            msg = '无效的JWT！'
            raise exceptions.AuthenticationFailed(msg)

        try:
            jwt_token = auth[1]
            payload = jwt.decode(jwt_token, key=settings.SECRET_KEY, algorithms="HS256")
            userid = payload.get('userid')
            try:
                user = OAUser.objects.get(pk=userid)
                return user, jwt_token
            except Exception:
                msg = '用户信息错误！'
                raise exceptions.AuthenticationFailed(msg)
        except UnicodeError:
            msg = 'UnicodeError'
            raise exceptions.AuthenticationFailed(msg)
        except jwt.ExpiredSignatureError:
            msg = 'token已过期！'
            raise exceptions.AuthenticationFailed(msg)
